package com.hao.yygh.msm.controller;

import com.hao.yygh.common.result.Result;
import com.hao.yygh.msm.service.MsmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/api/msm")
public class MsmApiController {
    @Autowired
    private MsmService msmService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @GetMapping("send/{email}")
    public Result sendCode(@PathVariable String email){
        String chackCode=String.valueOf(new Random().nextInt(899999)+100000);
        String content="您的验证码为："+chackCode;
        try{
            msmService.send(email,content);
            redisTemplate.opsForValue().set(email,chackCode,2, TimeUnit.MINUTES);
        }catch (Exception e){
            return Result.fail();
        }
        return Result.ok(chackCode);
    }
}
